package client

import (
	"fmt"
	"gitee.com/shijun123456/kcgin"
	"gitee.com/shijun123456/srpcinterface/client/utils"
	"github.com/flyaways/pool"
	redis2 "github.com/tal-tech/go-zero/core/stores/redis"
	"github.com/tal-tech/go-zero/zrpc"
	"google.golang.org/grpc"
	"log"
	"time"
)

//基础客户端信息
//更新
//测试信息
type DefaultFiler struct {
	Cli zrpc.Client
}

func NewDefaultFiler(cli zrpc.Client) *DefaultFiler {
	return &DefaultFiler{Cli: cli}
}

type Client struct {
	c       *grpc.ClientConn
	grpcpol *pool.GRPCPool
}
type Service interface {
	GetServiceName() string
}

func New() *Client {
	return &Client{}
}

func (cli *Client) Conn() *grpc.ClientConn {
	return cli.c
}
func (cli *Client) Close() *Client {
	cli.c.Close()
	return cli
}

var redisCli *redis2.Redis
var clientPool *utils.RpcClientPool

func init() {
	redisCli = redis2.New(GenerateAddress(), redis2.WithPass(kcgin.AppConfig.String("redis.pass")))
	clientPool = new(utils.RpcClientPool)
}

//根据servicename 名称查询address
func (cli *Client) consulByServiceName(service Service) (string, error) {
	if extis, err := cli.pool(service.GetServiceName()); err == nil {
		if len(extis) > 0 {
			return extis, nil
		} else {
			client := utils.DefaultConsulClient()
			address, err := client.ServiceFindByMicro(service.GetServiceName())
			expire := kcgin.AppConfig.DefaultInt("redis.timeout", 3600)
			isCache := kcgin.AppConfig.DefaultBool("redis.cache",false)
			if isCache {
				//是否需要缓存地址 如果没有则 不需要进行缓存信息
				cli.set(service.GetServiceName(), address, expire)
			}
			return address, err
		}
	} else {
		return "", err
	}

}
func GenerateAddress() string {
	host := kcgin.AppConfig.String("redis.host")
	port := kcgin.AppConfig.DefaultInt("redis.port", 6379)
	return fmt.Sprintf("%s:%d", host, port)
}
func (cli *Client) generateAddress() string {
	host := kcgin.AppConfig.String("redis.host")
	port := kcgin.AppConfig.DefaultInt("redis.port", 6379)
	return fmt.Sprintf("%s:%d", host, port)
}
func (cli *Client) pool(key string) (string, error) {
	return redisCli.Get(key)
}
func (cli *Client) set(key string, value string, expire int) error {
	redisCli.Expire(key, expire)
	return redisCli.Set(key, value)
}
func (cli *Client) Init(service Service) *Client {
	target, err := cli.consulByServiceName(service)
	if err != nil {
		panic(err)
	}
	options := &pool.Options{
		InitTargets:  []string{target},
		InitCap:      10,
		MaxCap:       30,
		DialTimeout:  time.Second * 5,
		IdleTimeout:  time.Second * 60,
		ReadTimeout:  time.Second * 5,
		WriteTimeout: time.Second * 5,
	}
	p, err := pool.NewGRPCPool(options, grpc.WithInsecure(),grpc.WithTimeout(time.Second*10))
	if err != nil {
		log.Printf("%#v\n", err)
	}
	if p == nil {
		log.Printf("p= %#v\n", p)
	}
	cli.grpcpol = p
	conn, err := p.Get()
	if err != nil {
		log.Printf("%#v\n", err)
	}
	cli.c = conn
	log.Printf("len=%d\n", p.IdleCount())
	return cli
}
func (cli *Client) PoolClose() {
	cli.grpcpol.Close()
}
func (cli *Client) PutConn(conn *grpc.ClientConn) {
	cli.grpcpol.Put(conn)
}
